%%%----------------------------------------------------------------------
%%% File    : master_pro_req.erl
%%% Author  : Sunny <sunjiangangok@gmail.com>
%%% Purpose : Process nc's message
%%% Created : 8 Apr 2011 by Sunny <sunjiangangok@gmail.com>
%%% Id      :
%%%----------------------------------------------------------------------

-module(master_pro_req).
-author('sunjiangangok@gmail.com').

-behaviour(gen_fsm).

%% External exports
-export([pro_start/1]).

%% gen_fsm callbacks
-export([processing/2,
	processed/2,
	init/1,
	handle_event/3,
	handle_sync_event/4,
	code_change/4,
	handle_info/3,
	terminate/3]).

%% Include hrl files
-include("record.hrl").

%%%----------------------------------------------------------------------
%%% External function
%%%----------------------------------------------------------------------
pro_start(MASTER_VM_START) ->
	start(MASTER_VM_START),
	operation(MASTER_VM_START),
	ok.

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start(MASTER_VM_START) ->
%	gen_fsm:start_link({local, master_pro_req}, master_pro_req, MASTER_VM_START, []).
	#master_vm_start{fsmname=FsmName, command=_Command, nc=_Nc, vm_num=_VM_num} = MASTER_VM_START,
	gen_fsm:start({local, FsmName}, master_pro_req, MASTER_VM_START, []).
operation(MASTER_VM_START) ->
	#master_vm_start{fsmname=FsmName, command=Command, nc=_Nc, vm_num=_VM_num} = MASTER_VM_START,
	gen_fsm:send_event(FsmName, {operation, Command}).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_fsm
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: init/1
%% Returns: {ok, StateName, StateData}          |
%%          {ok, StateName, StateData, Timeout} |
%%          ignore                              |
%%          {stop, StopReason}                   
%%----------------------------------------------------------------------
init(MASTER_VM_START) ->
%	process_flag(trap_exit, true),
	{ok, processing, MASTER_VM_START}.


%%----------------------------------------------------------------------
%% Func: StateName/2
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
processing({operation, Command}, MASTER_VM_START) ->
	case Command of
		start ->
			do_start(MASTER_VM_START),
			{next_state, processed, MASTER_VM_START, 4000};
		_Other ->
			io:format("There is some errors in your command,please check~n"),
			{stop, normal, MASTER_VM_START}
	end.
processed(timeout, MASTER_VM_START) ->
	io:format("Your request has been processed.~n"),
	{stop, normal, MASTER_VM_START}.

%%----------------------------------------------------------------------
%% Func: StateName/3
%% Returns: {next_state, NextStateName, NextStateData}            |
%%          {next_state, NextStateName, NextStateData, Timeout}   |
%%          {reply, Reply, NextStateName, NextStateData}          |
%%          {reply, Reply, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                          |
%%          {stop, Reason, Reply, NewStateData}                    
%%----------------------------------------------------------------------
%state_name(Event, From, StateData) ->
%    Reply = ok,
%    {reply, Reply, state_name, StateData}.



%%----------------------------------------------------------------------
%% Func: handle_event/3
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
handle_event(_Event, StateName, StateData) ->
    {next_state, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: handle_sync_event/4
%% Returns: {next_state, NextStateName, NextStateData}            |
%%          {next_state, NextStateName, NextStateData, Timeout}   |
%%          {reply, Reply, NextStateName, NextStateData}          |
%%          {reply, Reply, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                          |
%%          {stop, Reason, Reply, NewStateData}                    
%%----------------------------------------------------------------------
handle_sync_event(Event, From, StateName, StateData) ->
	{Event, From, StateName, StateData}.

code_change(_OldVsn, StateName, StateData, _Extra) ->
    {ok, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: handle_info/3
%% Returns: {next_state, NextStateName, NextStateData}          |
%%          {next_state, NextStateName, NextStateData, Timeout} |
%%          {stop, Reason, NewStateData}                         
%%----------------------------------------------------------------------
handle_info(Info, StateName, StateData) ->
	{Info, StateName, StateData}.

%%----------------------------------------------------------------------
%% Func: terminate/3
%% Purpose: Shutdown the fsm
%% Returns: any
%%----------------------------------------------------------------------
terminate(_Reason, _StateName, _StateData) ->
	ok.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------
do_start(MASTER_VM_START) ->
	io:format("This is do_start function~n"),
	#master_vm_start{fsmname=_FsmName, command=Command, nc=Nc, vm_num=VM_num} = MASTER_VM_START,
	NC_VM_START = #nc_vm_start{shell=Command, num=VM_num},
	rpc:call(Nc, nc, start_vm, [NC_VM_START]).
